package com.xxxx.yoyo.adminweb;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;


@WebServlet("/admin/typeAdd")
public class TypeAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式为UTF-8,解决请求参数中文乱码问题
		request.setCharacterEncoding ("UTF-8");
		
		//获取前端页面传递过来的参数name
		String name = request.getParameter ("name");
		//创建一个宠物分类(Type)
		Type type = new Type();
		type.setName(name);
		
		//添加宠物分类（添加到数据库）
		boolean isSuccess = typeService.addType(type);
		if(isSuccess){
			//提示成功
			request.setAttribute("msg","添加宠物大类成功！");
		}else {
			//提示失败
			request.setAttribute("msg","添加失败，宠物大类的名称过长，大类名称需要在20个字以内！");
		}
		
		//设置导航条上的[宠物大类管理]处于选中状态
		request.setAttribute("flag", 4);
		
		//获取[宠物分类]列表
		List<Type> typeList = typeService.getTypeList();
		//把cypeList存入request中，供/admin/type list.jsp页面使用
		request.setAttribute("typeList", typeList);
		
		//请求转发，跳转到/admin/type list.jsp页面
		request.getRequestDispatcher ("/admin/type_list.jsp").forward(request,response);
	}

}
